package com.jbp.controller;

import com.jbp.model.LoginParam;
import com.jbp.model.User;
import com.jbp.service.AccountService;
import com.jbp.utils.R;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Api(tags ="三方账户登录/注册接口" ,hidden = true)
@RestController
@RequestMapping(value = "rest/account")
public class AccountController {

    @Autowired
    private AccountService accountService;


    @ApiOperation(value="绑定微信",notes="绑定微信")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code",value = "登录类型",paramType = "query")
    })
    @PostMapping("/bindWechat")
    public R bindWechat(@RequestBody LoginParam loginParam,@RequestAttribute("userId") Integer userId){
        if (StringUtils.isEmpty(loginParam.getCode())){
            return R.error("参数错误");
        }
        loginParam.setUserId(userId);
        accountService.bindWechat(loginParam);
        return R.ok();
    }

    @ApiOperation(value="绑定qq",notes="绑定qq")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code",value = "登录类型",paramType = "query")
    })
    @PostMapping("/bindQq")
    public R bindQq(@RequestBody LoginParam loginParam,@RequestAttribute("userId") Integer userId){
        if (StringUtils.isEmpty(loginParam.getOpenid())
                || StringUtils.isEmpty(loginParam.getAccess_token())){
            return R.error("参数错误");
        }

        loginParam.setUserId(userId);
        accountService.bindQq(loginParam);
        return R.ok();
    }

    @ApiOperation(value="绑定手机号",notes="绑定手机号")
    @ApiResponses({
            @ApiResponse(code = 200,message = "登录成功",response = User.class),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile",value = "账号",paramType = "query"),
            @ApiImplicitParam(name="messageCode",value = "验证码",paramType = "query"),
    })
    @PostMapping("/bindPhone")
    public R bindPhone(@RequestBody LoginParam loginParam,@RequestAttribute("userId") Integer userId){
        if (StringUtils.isEmpty(loginParam.getMobile())){
            return R.error("参数错误");
        }

        loginParam.setUserId(userId);
        accountService.bindPhone(loginParam);
        return R.ok();
    }

    @ApiOperation(value="解绑",notes="解绑微信/QQ等")
    @ApiResponses({
            @ApiResponse(code = 200,message = "登录成功",response = User.class),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "accountType",value = "登录类型 wechat qq",paramType = "query")
    })
    @PostMapping("/unbindThirdAccount")
    public R unbindThirdAccount(@RequestBody LoginParam loginParam,@RequestAttribute("userId") Integer userId){
        loginParam.setUserId(userId);
        accountService.untieThirdAccount(loginParam.getAccountType(), userId);
        return R.ok();
    }
}
